AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_player.lua")
AddCSLuaFile("cl_hud.lua")
AddCSLuaFile("cl_scoreboard.lua")
AddCSLuaFile("vgui/Quakeboard.lua")
AddCSLuaFile("vgui/quakerow.lua")

include("ent_manager.lua")
include("filesystem.lua")

include("shared.lua")
include("sv_player.lua")

GM.PlayerSpawnTime = {}
Bots = {}

function GM:PlayerSetModel(ply)
   local mdl ="models/player/phoenix.mdl"
   util.PrecacheModel(mdl)
   ply:SetModel(mdl)
end

--[[---------------------------------------------------------
   Name: gamemode:Initialize( )
   Desc: Called immediately after starting the gamemode 
-----------------------------------------------------------]]
function GM:Initialize(  )


end
--[[---------------------------------------------------------
	Send Data to Client
-----------------------------------------------------------]]





/*
	BOTCHAT

	


*/


function BotChat()


end
--[[---------------------------------------------------------
   Name: gamemode:PlayerInitialSpawn()
   Desc:  
-----------------------------------------------------------]]
function GM:PlayerInitialSpawn(ply)
// >> newer one <<
ply.DataTable = {}
ply.DataTable["Rank"] = "Unknown"
ply.DataTable["flag"] = EF_AWARD_NONE
ply.DataTable["flagcount"] = 0
ply.DataTable["flagcount2"] = 0
ply.DataTable["lastkill"] = 0.00
ply.DataTable["Lastvictim"] = "bc"
ply.DataTable["jointime"] = CurTime()
ply.DataTable["SoundPlayed"] = false
ply:SetRank("none")

if(ply:IsBot()) then
	table.insert(Bots,{Bot = ply, Botname = ply:GetName(), Botnicename = "Duck[1]"})
end

end
--[[---------------------------------------------------------
   Name: gamemode:PlayerSpawn( )
   Desc: e 
-----------------------------------------------------------]]
function GM:PlayerSpawn( ply )
player_manager.SetPlayerClass( ply, "player_quake" )
ply:SetStepSize(18)
ply:SetWalkSpeed( 400 )
ply:SetJumpPower( 200 )
hook.Call("PlayerSetModel", GAMEMODE, ply)	
player_manager.RunClass( ply, "Loadout" )

end


function Sort()



end

--[[---------------------------------------------------------
   Name: gamemode:Think( )
   Desc: Called every frame
-----------------------------------------------------------]]
function GM:Think( )

end


--[[---------------------------------------------------------
   Name: gamemode:ShutDown( )
   Desc: Called when the Lua system is about to shut down
-----------------------------------------------------------]]
function GM:ShutDown( )

end

--[[---------------------------------------------------------
   Name: gamemode:DoPlayerDeath( )
   Desc: Carries out actions when the player dies 		 
-----------------------------------------------------------]]
function GM:DoPlayerDeath( victim, attacker, dmginfo )

local tab = {}
for k,v in ipairs(player.GetAll()) do
	table.insert(tab, {Frags = v:Frags(),Play = v})
end

table.SortByMember( tab, "Frags" )

	//ply:CreateRagdoll()
	victim:AddDeaths( 1 )
	victim:SetFlagCount(0)
	victim:SetFlagCount2(0)
	if ( attacker:IsValid() && attacker:IsPlayer() ) then
			if(tab[1].Play:GetName() == attacker:GetName()) then
			attacker:SetRank("1st")
			elseif(tab[2].Play:GetName() == attacker:GetName()) then
			attacker:SetRank("2nd")
			elseif(tab[3].Play:GetName() == attacker:GetName()) then
			attacker:SetRank("3rd")
			else
			attacker:SetRank("none")
			end
				umsg.Start("leader")
				umsg.Entity( attacker )
				umsg.End()
		if ( attacker == victim ) then
			attacker:AddFrags( -1 )
		else

			attacker:AddFrags( 1 )
			local imp = attacker.DataTable["flagcount"]
			local exe = attacker.DataTable["flagcount2"]
			if ( CurTime() - attacker.DataTable["lastkill"] < CARNAGE_REWARD_TIME ) then
			attacker:SetFlag(EF_AWARD_EXCELLENT)
			attacker:SetSoundPlayed(false)
			imp = imp + 1
			attacker:SetFlagCount(imp) 
			umsg.Start("KillSprite")
			umsg.Entity( attacker )
			umsg.Short(EF_AWARD_EXCELLENT)
			umsg.End()
			elseif(CurTime() - attacker.DataTable["lastkill"] > CARNAGE_REWARD_TIME) then
			attacker:SetFlag(EF_AWARD_NONE)
			attacker:SetSoundPlayed(true)
			BOOL = true
			end
			if ( CurTime() - attacker.DataTable["lastkill"] < CARNAGE_REWARD_TIME and attacker:GetActiveWeapon():GetClass() == "weapon_railgun" ) then
			//attacker.soundplayed = false;
			attacker:SetFlag(EF_AWARD_IMPRESSIVE)
			attacker:SetSoundPlayed(false)
			exe = exe + 1
			attacker:SetFlagCount2(exe)
			umsg.Start("KillSprite")
			umsg.Entity( attacker )
			umsg.Short(EF_AWARD_IMPRESSIVE)
			umsg.End()
			elseif(CurTime() - attacker.DataTable["lastkill"] > CARNAGE_REWARD_TIME and attacker:GetActiveWeapon():GetClass() != "weapon_railgun") then
			BOOL = true
			attacker:SetFlag(EF_AWARD_NONE)
			attacker:SetSoundPlayed(true)
			end
			attacker:SetLastVictim(victim:GetName())
			//attacker:ChatPrint(CARNAGE_REWARD_TIME .. " laskill was on " .. CurTime() - attacker.DataTable["lastkill"] )
			//attacker:ChatPrint("Victim was: "..attacker.DataTable["Lastvictim"].." ")

		end

		attacker:SetLastKillTime(CurTime())
	end
		if( CurTime() > 1+CurTime()) then BOOL = false end
		if(attacker:IsValid() && attacker:IsPlayer()) then
		
				victim:splash(dmginfo)

		
		end
end


--[[---------------------------------------------------------
   Name: gamemode:EntityTakeDamage( entity, inflictor, attacker, amount, dmginfo )
   Desc: The entity has received damage	 
-----------------------------------------------------------]]
function GM:EntityTakeDamage( ent, inflictor, attacker, amount, dmginfo )
end


--[[---------------------------------------------------------
   Name: gamemode:CreateEntityRagdoll( entity, ragdoll )
   Desc: A ragdoll of an entity has been created
-----------------------------------------------------------]]
function GM:CreateEntityRagdoll( entity, ragdoll )
end


-- Set the ServerName every 30 seconds in case it changes..
-- This is for backwards compatibility only - client can now use GetHostName()
local function HostnameThink()

	SetGlobalString( "ServerName", GetHostName() )

end

timer.Create( "HostnameThink", 30, 0, HostnameThink )

--[[---------------------------------------------------------
	Show the default team selection screen
-----------------------------------------------------------]]
function GM:ShowTeam( ply )

	if (!GAMEMODE.TeamBased) then return end
	
	local TimeBetweenSwitches = GAMEMODE.SecondsBetweenTeamSwitches or 10
	if ( ply.LastTeamSwitch && RealTime()-ply.LastTeamSwitch < TimeBetweenSwitches ) then
		ply.LastTeamSwitch = ply.LastTeamSwitch + 1;
		ply:ChatPrint( Format( "Please wait %i more seconds before trying to change team again", (TimeBetweenSwitches - (RealTime()-ply.LastTeamSwitch)) + 1 ) )
		return false
	end
	
	-- For clientside see cl_pickteam.lua
	ply:SendLua( "GAMEMODE:ShowTeam()" )

end


function sendplayed( pl )
umsg.Start("sendplayed",pl)
umsg.Bool(pl.DataTable["SoundPlayed"])
umsg.End()
end

function sendrank( pl )
umsg.Start("sendrank",pl)
umsg.String(pl.DataTable["Rank"])
umsg.End()
end
function sendflag( pl )
umsg.Start("sendflag",pl)
umsg.Short(pl.DataTable["flag"])
umsg.End()
end
function sendflagcount( pl )
umsg.Start("sendflagcount",pl)
umsg.Short(pl.DataTable["flagcount"])
umsg.End()
end
function sendflagcount2( pl )
umsg.Start("sendflagcount2",pl)
umsg.Short(pl.DataTable["flagcount2"])
umsg.End()
end
function sendlastkilltime( pl )
umsg.Start("sendlastkilltime",pl)
umsg.Float(pl.DataTable["lastkill"])
umsg.End()
end
function sendlastvictim( pl )
umsg.Start("sendlastvictim",pl)
umsg.String(pl.DataTable["Lastvictim"])
umsg.End()
end